package com.qk.management.statemachine.config.clueconfig;

import com.qk.management.statemachine.event.ClueEvent;
import com.qk.management.statemachine.status.ClueStatus;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.Arrays;
import java.util.HashSet;

@Configuration
@EnableStateMachineFactory(name = "clueStateMachine")
public class ClueStateMachineConfig extends EnumStateMachineConfigurerAdapter<ClueStatus, ClueEvent> {

    @Override
    public void configure(StateMachineStateConfigurer<ClueStatus, ClueEvent> states) throws Exception {
        states.withStates()
                .initial(ClueStatus.WAITING)
                .states(new HashSet<>(Arrays.asList(ClueStatus.values())));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<ClueStatus, ClueEvent> transitions) throws Exception {
        transitions
                .withExternal()
                    .source(ClueStatus.WAITING).target(ClueStatus.ASSIGNED).event(ClueEvent.ASSIGN)
                .and()
                .withExternal()
                    .source(ClueStatus.ASSIGNED).target(ClueStatus.FOLLOWING).event(ClueEvent.FOLLOW_UP)
                .and()
                .withExternal()
                    .source(ClueStatus.FOLLOWING).target(ClueStatus.FOLLOWING).event(ClueEvent.FOLLOW_UP) // 可以自循环
                .and()
                .withExternal()
                    .source(ClueStatus.FOLLOWING).target(ClueStatus.PSEUDO_CLUES).event(ClueEvent.MARK_FALSE)
                .and()
                .withExternal()
                    .source(ClueStatus.ASSIGNED).target(ClueStatus.BUSINESS).event(ClueEvent.CONVERT_TO_BUSINESS)
                .and()
                .withExternal()
                    .source(ClueStatus.FOLLOWING).target(ClueStatus.BUSINESS).event(ClueEvent.CONVERT_TO_BUSINESS)
        ;
    }
}
